﻿package Cain.mc_cesh
{
	import caurina.transitions.properties.DisplayShortcuts;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import Cain.mc_cesh.*;
	import Cain.all_mk;
	import flash.events.Event;
	import caurina.transitions.Tweener;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.ui.Mouse;
	/**
	 * 動態拍照 傳入的物件 可以根據滑鼠移動
	 * @author cain@ 2009
	 */
	public class Fn extends EventDispatcher 
	{
		private var mc:DisplayObject;
		private var fn:DisplayObject;
		private var bk:all_mk;
		private var	ca:Cesher_fn;
		/**
		 * 動態拍照 傳入的物件 可以根據滑鼠移動
		 * @param	_mc 被拍的物件
		 * @param	_fn 拍的範圍
		 */
		public function Fn(_mc:DisplayObject,_fn:DisplayObject) 
		{
			mc = _mc;
			fn = _fn;
			init();
		}
		
		private function init():void
		{
			
		}
		/**
		 * 範圍開始跟隨滑鼠
		 */
		public function start():void 
		{
			Mouse.hide();
			fn.addEventListener(Event.ENTER_FRAME, fn_play);
			bk = new all_mk(fn);
			bk.addEventListener(MouseEvent.MOUSE_DOWN, bk_mouse_down);
		}
		
		private function bk_mouse_down(e:MouseEvent):void 
		{
			bk.removeEventListener(MouseEvent.MOUSE_DOWN, bk_mouse_down);
			bk.unload();
			ca = new Cesher_fn(mc, fn);
			//ca.shoit();//======================================測試顯示
			stop();
		}
		/**
		 * 取得拍照內容
		 * @return 取得拍照內容
		 */
		public function bitmapData():BitmapData 
		{
			return ca.bitmapData();
		}
		private function fn_play(e:Event):void 
		{
			var fn_MousePoint:Point = new Point(fn.parent.mouseX, fn.parent.mouseY);
			var fn_RootMousePoint:Point = fn.parent.localToGlobal(fn_MousePoint);
			//var mc_Point:Point = mc.globalToLocal(fn_RootMousePoint);
			var fn_moveX:Number = fn.parent.globalToLocal(fn_RootMousePoint).x
			var fn_moveY:Number = fn.parent.globalToLocal(fn_RootMousePoint).y
			var mc_rootTopPoint:Point = mc.localToGlobal(new Point(mc.getRect(mc).x, mc.getRect(mc).y)); 
			var mc_rootfootPoint:Point = mc.localToGlobal(new Point(mc.getRect(mc).x + mc.width, mc.getRect(mc).y + mc.height));
			var fn_rootTopPoint:Point = new Point(fn_RootMousePoint.x+fn.getRect(fn).x,fn_MousePoint.y+fn.getRect(fn).y);
			var fn_rootfootPoint:Point = new Point(fn_RootMousePoint.x+fn.getRect(fn).x+fn.width,fn_MousePoint.y+fn.getRect(fn).y+fn.height);
			if(fn_rootTopPoint.x>mc_rootTopPoint.x && fn_rootTopPoint.y>mc_rootTopPoint.y && fn_rootfootPoint.x< mc_rootfootPoint.x && fn_rootfootPoint.y< mc_rootfootPoint.y)
			{
				Tweener.addTween(fn, { x:fn_moveX, y:fn_moveY, time:2 } );
			}
		}
		/**
		 * 按下快門,並停止拍照
		 */
		public function stop():void 
		{
			Mouse.show();
			fn.removeEventListener(Event.ENTER_FRAME, fn_play);
			dispatchEvent(new Event("cesh"));
		}
	}
	
}